package com.sg.service.base.dataassets.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.dataassets.req.BatchQueryAlbumReq;
import com.sg.dto.base.dataassets.req.QueryAlbumDetailReq;
import com.sg.dto.base.dataassets.req.QueryAlbumListReq;
import com.sg.entity.OmsAlbum;
import com.sg.service.base.dataassets.MOmsAlbumService;
import com.wicket.okrdataassets.common.dto.FieldInfo;
import com.wicket.okrdataassets.common.exception.BizException;
import com.wicket.okrdataassets.dal.mapper.mbg.OmsAlbumMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAlbumServiceImpl
        extends ServiceImpl<OmsAlbumMapper, OmsAlbum>
        implements MOmsAlbumService {

    @Autowired
    OmsAlbumMapper omsAlbumMapper;

    /**
     * 8-02-01新增专辑[10294]
     * gen by moon at 11/11/2024, 1:24:07 AM
     */
    @Trace(operationName = "8-02-01新增专辑")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAlbum(OmsAlbum omsAlbum) {
        omsAlbum.setAlbumId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAlbum.setCreateOriginalRoleMemberId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecOriginalRoleMemberId());//CURRENT_ORIGINAL_ROLE_MEMBERID
        omsAlbum.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbum.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAlbum.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbum.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbum.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAlbum.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAlbum.setIsArchive("FALSE");
        omsAlbum.setIsValid("FALSE");
        omsAlbum.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAlbum.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAlbum)) {
            return omsAlbum.getAlbumId();
        } else {
            return "-1";
        }

    }

    /**
     * 8-02-01修改专辑[10297]
     * gen by moon at 11/11/2024, 1:24:08 AM
     */
    @Trace(operationName = "8-02-01修改专辑")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAlbum(OmsAlbum omsAlbum) {
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbum.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbum.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAlbum.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbum.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAlbum.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbum.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAlbum);
    }

    /**
     * 8-02-01删除专辑[10298]
     * gen by moon at 11/11/2024, 1:24:09 AM
     */
    @Trace(operationName = "8-02-01删除专辑")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAlbum(String albumId) {

        return super.removeById(albumId);
    }

    /**
     * 8-02-01查询专辑详情[10299]
     * gen by moon at 11/11/2024, 1:24:12 AM
     */
    @Trace(operationName = "8-02-01查询专辑详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAlbum queryAlbumDetail(QueryAlbumDetailReq reqDto) {

        try {
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsAlbum> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAlbum> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAlbum retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 8-02-01查询专辑列表（按创建时间降序）[10300]
     * gen by moon at 11/11/2024, 1:24:15 AM
     */
    @Trace(operationName = "8-02-01查询专辑列表（按创建时间降序）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAlbum> queryAlbumList(QueryAlbumListReq reqDto) {

        try {
            QueryWrapper<OmsAlbum> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAlbum.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAlbum.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAlbum.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAlbum>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 8-02-01批量查询专辑列表[10302]
     * gen by moon at 11/11/2024, 1:24:18 AM
     */
    @Trace(operationName = "8-02-01批量查询专辑列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAlbum> batchQueryAlbum(BatchQueryAlbumReq reqDto) {

        try {
            QueryWrapper<OmsAlbum> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getAlbumList() == null || reqDto.getAlbumList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("album_id", reqDto.getAlbumList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAlbum.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAlbum.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAlbum.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAlbum>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 8-02-01新增默认专辑[10522]
     * gen by moon at 4/10/2025, 10:22:23 AM
     */
    @Trace(operationName = "8-02-01新增默认专辑")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addDefaultAlbum(OmsAlbum omsAlbum) {
        omsAlbum.setAlbumId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAlbum.setCreateOriginalRoleMemberId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecOriginalRoleMemberId());//CURRENT_ORIGINAL_ROLE_MEMBERID
        omsAlbum.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbum.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbum.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbum.setCreateTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        if (omsAlbum != null && omsAlbum.getSubjectLifeCycle() != null && omsAlbum.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbum.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAlbum.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAlbum.setIsArchive("FALSE");
        omsAlbum.setIsValid("FALSE");
        omsAlbum.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAlbum.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAlbum)) {
            return omsAlbum.getAlbumId();
        } else {
            return "-1";
        }

    }
}
